/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package generadorcommun;

import java.sql.*;
import java.sql.DriverManager;
//import com.mysql.jdbc.Connection;
//import com.mysql.jdbc.Statement;
//import java.sql.DriverManager;
//import java.sql.ResultSet;
//import java.sql.SQLException;
 
/**
 *
 * @author Martin
 */
public class DataBase {

    private static DataBase instance;
    //jdbc:mysql://localhost:3306
    //jdbc:mysql://localhost:3306
    //root
    //root
    //test
    private Connection conexion;
    private String url = "";
    private String urlDefault = "";
    private String user = "";
    private String pass = "";

    private DataBase() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (Exception e) {
            throw new UnsupportedOperationException(e.toString());
        }
    }

    public static DataBase getInstance() {
        if (instance == null) {
            instance = new DataBase();
        }
        return instance;
    }

    public String getUrlDefault() {
        return urlDefault;
    }

    public void setUrlDefault(String urlDefault,String db) {
        this.urlDefault = "jdbc:mysql://"+urlDefault+'/'+db;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url ="jdbc:mysql://"+ url+'/';
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public void crearConexion(String nombreProy) throws SQLException {
        this.conexion = DriverManager.getConnection(url+nombreProy.toLowerCase(), user, pass);
    }

    public void cerrarConexion() throws SQLException {
        this.conexion.close();
    }

    public void crearDataBase(String nombre) throws SQLException {
        String nom = nombre.toLowerCase();
        Connection con = DriverManager.getConnection(urlDefault, user, pass);
        Statement st = con.createStatement();
        st.executeUpdate("CREATE DATABASE " + nom);
        con.close();
    }

    public void crearTabla(String query,String nombreProy) throws SQLException {
        crearConexion(nombreProy);
        Statement st = this.conexion.createStatement();
        st.executeUpdate(query);
        st.close();
        cerrarConexion();
    }
}
